
//实现线程池

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class MyThreadPoolExecutor {
    //存储线程的列表
    private List<Thread> threadList = new ArrayList<>();
    //任务队列
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);
    //n 线程数
    public MyThreadPoolExecutor(int n){
        for(int i =0;i<n;i++) {
            Thread t = new Thread(()-> {
                //把任务队列中的任务不停的取出来，并进行执行
                try {
                    //take 带有阻塞功能，如果队列为空，就会阻塞
                    Runnable runnable = queue.take();
                    //取出任务 并执行
                    runnable.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
            threadList.add(t);
        }
    }

    //submit方法
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }


}
